import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
 
public class Lib {
	private String path;
	private String words = "";
	private String partword;
	private HashMap<String, Integer> wordsmap = new HashMap<String, Integer>();
 
	public WordStatistics() {
 
	}
 
	public WordStatistics(String path) {
		this.path = path;
	}
 
	public void setPath(String path) {
		this.path = path;
	}
 
	public  void statistics() {
		try {
			FileInputStream inputStream = new FileInputStream(new File(path));
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(inputStream));
			while ((partword = bufferedReader.readLine()) != null) {
				words += partword;
			}
			StringTokenizer tokenizer = new StringTokenizer(words,
					"\n\r\t .,\"?!:'");
			System.out.println("单词数量" + tokenizer.countTokens());
			while (tokenizer.hasMoreElements()) {
				String key = tokenizer.nextToken();
				if (!isMeaning(key)) {
					key = tokenizer.nextToken();
				} else {
					int value = 1;
					// 添加筛选，将无效词汇去除
					if (wordsmap.get(key) != null) {
						value = (Integer) wordsmap.get(key).intValue();
						value++;
						wordsmap.put(key, value);
					} else {
						wordsmap.put(key, new Integer(value));
 
					}
				}
			}
			// 对集合根据Value值排序
			Set<?> sortset = wordsmap.entrySet();
			Map.Entry[] entrys = (Map.Entry[]) sortset
					.toArray(new Map.Entry[sortset.size()]);
			// 排序
			Arrays.sort(entrys, new Comparator<Object>() {
 
				public int compare(Object o1, Object o2) {
					Long key1 = Long.valueOf(((Map.Entry) o1).getValue()
							.toString());
					Long key2 = Long.valueOf(((Map.Entry) o2).getValue()
							.toString());
					return key1.compareTo(key2);
				}
 
			});
 
			// 遍历Map.Entry[]
			System.out.println("高频词汇前10");
			for (int i = entrys.length - 1; i >= entrys.length - 10; i--) {
				System.out.println(entrys[i].getKey() + ":"
						+ entrys[i].getValue());
			}
			bufferedReader.close();
			inputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
     public static void wc(InputStream f) throws IOException {         
         int c = 0;          
         boolean lastNotWhite = false;         
         String whiteSpace = " \t\n\r";         
         while ((c = f.read()) != -1) {             
             chars++;              
             if (c == '\n') {                 
                 lines++;             
                 }              
                 if (whiteSpace.indexOf(c) != -1) {                 
                     if (lastNotWhite) {                     
                         words++;                 
                         }                  
                         lastNotWhite = false;             
                         } else {                  
                             lastNotWhite = true;             
                             }         
                             }     
                             }        
                             
                       }
	}
